package com.hefei.garden.controller;

import com.hefei.garden.config.token.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: devil
 * @Date: 2022/8/17 23
 * @Description: 接口幂等性测试
 */
@RestController
public class TokenController {

    @Autowired
    private TokenUtil tokenUtil;

    /**
     * 测试用户（实际从 权限架构中，redis 或者 mysql 获取）
     */
    private final String USER_NAME = "ChenYuXiu";

    /**
     * 获取token
     *
     * @return String
     */
    @GetMapping("/token")
    public String getToken() {
        return tokenUtil.generateToken(USER_NAME);
    }

    /**
     * 接口幂等性测试接口
     *
     * @param token
     * @return String
     */
    @PostMapping("/test")
    public String test(@RequestHeader(value = "token") String token) {
        boolean result = tokenUtil.validToken(token, USER_NAME);
        return result ? "正常调用" : "重复调用";
    }

}
